/*
 *   Copyright 1999-2016 Asiainfo Technologies(China),Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

package com.asiainfo.redis.repository;

import com.asiainfo.redis.entity.BusiDataRule;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * @version v 1.0 on 2016/6/22 17:52
 * @auther william.xu
 */
public interface BusiDataRuleRepository extends JpaRepository<BusiDataRule, Long>, JpaSpecificationExecutor<BusiDataRule> {

    @Query(value = "select count(id) from REDIS_BUSI_DATA_RULE where BUSI_TYPE_ID=:busiTypeId", nativeQuery = true)
    public int findCountByBusiTypeId(@Param("busiTypeId") Long busiTypeId);

    @Query(value = "select count(id) from REDIS_BUSI_DATA_RULE where DATASOURCE_ID=:busiDatasourceId", nativeQuery = true)
    public int findCountByBusiDatasourceId(@Param("busiDatasourceId") Long busiDatasourceId);

    public List<BusiDataRule> findByClusterIdAndBusiTypeIdAndScheduleFlag(Long clusterId, Long busiTypeId, Integer scheduleFlag);

    public List<BusiDataRule> findByClusterIdAndBusiTypeIdAndKeyPrefix(Long clusterId, Long busiTypeId, String keyPrefix);

    @Query(value = "select t from BusiDataRule t where t.configName like %?1% and t.keyPrefix like %?2% " +
            "and t.busiTypeId=?3 and t.clusterId=?4 ")
    public Page<BusiDataRule> findByConfigNameAndkeyPrefixAndBusiTypeId(String configName, String keyPrefix,
                                                                        Long busiTypeId, Long clusterId, Pageable pageable);

    //public Page<BusiDataRule> findByXxx(Specification<BusiDataRule> spec);//动态sql查询
}
